package com.dobby.pea.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dobby.pea.common.BaseContext;
import com.dobby.pea.common.R;
import com.dobby.pea.dto.PurchaseDto;
import com.dobby.pea.entity.tbPurchaseBid;
import com.dobby.pea.entity.tbUser;
import com.dobby.pea.service.PurchaseBidService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Dobby
 * @version 1.0
 */

@RestController
@RequestMapping("/api/purchasebid")
@Slf4j
public class PurchaseBidController {

    @Autowired
    PurchaseBidService purchaseBidService;


    /**
     * 参加竞标
     * @param tbPurchaseBid
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody tbPurchaseBid tbPurchaseBid){
        log.info("参加竞标-----{}",tbPurchaseBid);
        purchaseBidService.save(tbPurchaseBid);
        return R.success("发布竞标");
    }

    /**
     * 修改竞标
     * @param tbPurchaseBid
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody tbPurchaseBid tbPurchaseBid){
        log.info("修改竞标-----{}",tbPurchaseBid);
        purchaseBidService.updateById(tbPurchaseBid);
        return R.success("修改成功");
    }

    /**
     * 退出竞标
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<String> out(Long id){
        log.info("退出竞标-----{}",id);
        UpdateWrapper<tbPurchaseBid> tbPurchaseBidUpdateWrapper = new UpdateWrapper<>();
        tbPurchaseBidUpdateWrapper.eq("id",id);
        tbPurchaseBidUpdateWrapper.set("is_deleted",1);
        purchaseBidService.update(tbPurchaseBidUpdateWrapper);
        return R.success("退出成功");
    }

    /**
     * 根据用户查看该用户的竞标(分页查询，返回常规数据类型)
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/pageUser")
    public R<Page> pageWithUser(int page, int pageSize){
        log.info("根据用户查看该用户的竞标(分页查询)-----{}{}",page,pageSize);
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<tbPurchaseBid> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(tbPurchaseBid::getCreateUser, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(tbPurchaseBid::getUpdateTime);

        purchaseBidService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 根据采购id查询采购数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<tbPurchaseBid> getWithId(Long id){
        log.info("根据采购id查询采购数据-----{}",id);
        tbPurchaseBid tbPurchaseBid = purchaseBidService.getById(id);
        return R.success(tbPurchaseBid);
    }

}
